<?php
require('library/fpdf/fpdf.php'); 
class SchedulePDF extends FPDF
{
	protected $footerText = 'Gedownload van http://www.vasp.nl';
	
	protected $gameCount = 0;
	
	protected $lastTime = null;
	
	public function addPageWithTitleAndLocation($title, $location)
	{
		$this->AddPage();
		$this->SetFont('Arial','B',13);
		$this->Cell(40,10,$title);		
		$this->Ln(8);		
		$this->SetFont('Arial','B',10);
		$this->Cell(158,10,'Locatie: '.$location);
		
	}
	
	/**
	 * @param string $text
	 * @return void
	 */
	public function setFooterText($text)
	{
		$this->footerText = $text;
	}
	
	/**
	 * (non-PHPdoc)
	 * @see FPDF::AddPage()
	 */
	public function AddPage()
	{
		$this->gameCount = 0;
		$this->lastTime = null;
		return parent::AddPage();
	}
	
	/**
	 * @return string
	 */
	public function getFooterText()
	{
		return $this->footerText;
	}
	
	public function addFieldTitle($field)
	{
		$this->Ln(15);		
		$this->SetFont('Arial','B', 25);
		$this->Cell(40,10, 'Veld ' . $field);		
		$this->Ln(5);
	}
	
	public function addGamesTableHeader($includeField = true)
	{
		$this->lastGame = null;
		$this->SetFont('Arial','B', 10);
		$this->Cell(18,5,'Tijd');
		if($includeField)
		{
			$this->Cell(10,5,'Veld');			
		}
		$this->Cell(40,5,'Soort');
		$this->Cell(40,5,'Thuis');
		$this->Cell(10,5,'');
		$this->Cell(40,5,'Uit');
		$this->Ln(10);
	}	

	public function renderGamesPage(array $aGames, $includeField = true)
	{
		$this->Ln(15);
		
		$this->addGamesTableHeader($includeField);
		
		$fieldsGames = array();
		$fields = 0;
		foreach($aGames as $oGame)
		{			
			$this->addGameRow($oGame, $includeField);
			$oGame = Game::getByNefubId($oGame->nefub_id);
			if($includeField)
			{
				$field = $oGame->field ? $oGame->field : 1;				
				if(!is_array($fieldsGames[$field]))
				{
					$fieldsGames[$field] = array();
					$fields++;
				}
				$fieldsGames[$field][] = $oGame; 			
			}
		}
		
		if($fields <= 1)
		{
			$fieldsGames = array();
		}
		ksort($fieldsGames);		
		return $fieldsGames;
	}
	
	public function addGameRow(Game $oGame, $includeField = true)
	{
		//A4 = 210x297mm
		$oGame = Game::getByNefubId($oGame->nefub_id);
		$field = $oGame->field ? $oGame->field : 1;
		
		// Break als we al weten dat we de pagina gaan overschrijden
		$rowHeight = (int)$includeField * 30;
		if($this->y > 297 - $rowHeight)
		{
			$this->AddPage();
		}
		
		$gameType = $oGame->getCompetition()->getGender()->name .' '. $oGame->getCompetition()->getGenre()->name;
		$team1 = utf8_decode($oGame->getTeam1()->name);
		$team2 = utf8_decode($oGame->getTeam2()->name);
		
		if($oGame->getFirstReferee())
		{
			$oGame = new Game($oGame->getId());
			$oReferee = $oGame->getFirstReferee();
			$referee = utf8_decode($oReferee->getName());
			
			if(!$oReferee->getPerson())
			{			
				$referee .= ' ('. $oReferee->getTeam()->getCompetition()->getGender()->name;
				$referee .= ' '. $oReferee->getTeam()->getCompetition()->getGenre()->name .')';
			}
		}
		else
		{
			$referee = 'Onbekend';
		}
		
		//297
		
		$this->SetFont('Arial','B', 11);
		$this->Cell(18,5, $oGame->getFormattedTime());
		
		$columns = 11;
		
		if($includeField)
		{
			$this->Cell(10,5, $field);
			$columns +=1;
		}
		$this->SetFont('Arial','B', 10);
		$this->Cell(40,5,$gameType);
		$this->Cell(40,5,$team1);
		$this->Cell(10,5,'');
		$this->Cell(40,5,$team2);
		$this->Ln(5);

		
		$this->SetFont('Arial','', 10);
		if($includeField)
		{
			$this->Cell(68,5,'');
		}
		else
		{
			$this->Cell(58,5,'');				
		}
		$this->Cell(70,5, 'Wedstrijdleiding: '.$referee);
		$this->Ln(5);
		$this->Cell(158,5,' ');
		$this->Ln(5);
		
		$this->gameCount +=1;
		$this->lastGame = $oGame;
	}
	
	public function Footer()
	{
		// Position at 1.5 cm from bottom
	    $this->SetY(-15);
	    // Arial italic 8
	    $this->SetFont('Arial','I',8);
	    // Page number
	    $this->Cell(0,10,$this->footerText,0,0,'C');
	}
}